package com.itheima.Test01.edu.info.manager.teacher.dao;

import com.itheima.Test01.edu.info.manager.teacher.domain.Student;

public class StudentDao {
    private static Student[] arr = new Student[5];
    public static boolean addStudent(Student stu) {
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null){
                index = i;
                break;
            }
        }
        if (index != -1){
            arr[index] = stu;
            return true;
        }else {
            return false;
        }
    }

    public static boolean isExists(String id) {
        boolean flag = true;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null && arr[i].getId().equals(id) ){
                flag = false;
                break;
            }else {
                flag = true;
            }
        }
        return flag;
    }

    public static Student[] QueryStudent() {
        return arr;
    }

    public static void delentStudent(String id) {
        int index = getIndex(arr, id);
        if (index != -1){
            arr[index] = null;
        }

    }

    public static int getIndex(Student[] arr, String id){
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].getId().equals(id)){
                index = i;
                return index;
            }
        }
        return index;
    }

    public static void updateStudent(Student stu,String id) {
        int index = getIndex(arr, id);
        arr[index] = stu;
    }
}
